package linuxbox.resource.logic;

import java.util.*;

import linuxbox.bean.*;
import linuxbox.util.*;

import linuxbox.resource.bean.*;

public class BoxResourceCat
{
  public BoxResourceCatEntry get(BaseIntKey key)
  {
    if (key.getID() == 0)
      return null;
    BoxResourceCatEntry entry = null;
    try
    {
      BoxResourceCatEntryBean bean = new BoxResourceCatEntryBean(key);
      bean.load();
      entry = new BoxResourceCatEntry();
      entry.copyFrom(bean);
    }
    catch (Exception e)
    {
      //News: Log something?
      e.printStackTrace();
    }
    return entry;
  }

  public BoxResourceCatEntry add()
  {
    try
    {
      BoxResourceCatEntryBean bean = new BoxResourceCatEntryBean(new BaseIntKey(0));
      bean.create();

      BoxResourceCatEntry entry = new BoxResourceCatEntry();
      entry.copyFrom(bean);
      return entry;
    }
    catch (Exception e)
    {
      //TODO: Log something?
      e.printStackTrace();
    }
    return null;
  }

  public boolean remove(BaseIntKey key)
  {
    try
    {
      BoxResourceCatEntryBean bean = new BoxResourceCatEntryBean(key);
      bean.remove();
    }
    catch (Exception e)
    {
      //TODO: Log something?
      e.printStackTrace();
      return false;
    }
    return true;
  }

  public boolean update(BoxResourceCatEntry entry)
  {
    try
    {
      BoxResourceCatEntryBean bean = new BoxResourceCatEntryBean(new BaseIntKey(entry.getID()));
      bean.copyFrom(entry);
//System.out.println(bean);
      bean.store();
    }
    catch (Exception e)
    {
      //TODO: Log something?
      e.printStackTrace();
      return false;
    }
    return true;
  }

  public String getCategoryString(int id)
  {
    BoxResourceCatEntry entry = get(new BaseIntKey(id));
    if ( entry != null)
      return entry.getName();
    else
      return "";
  }

  public int getTotalPages(int entriesPerPage)
  {
    int pages = 1;
    try
    {
     BoxResourceEntryBean bean = new BoxResourceEntryBean(null,null);
     pages = ((bean.getCountBySql("") - 1) / entriesPerPage) + 1;
    }
    catch (Exception e)
    {
     e.printStackTrace();
    //TODO: Log
    }
    return pages;
  }

  public Iterator getAllCategories()
  {
  	String sql = " order by id";
    return getBySql(sql);
  }

  public Iterator getResourceCats(String SORTBY, boolean sortAsc, int from , int limit)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" order by ");
    sql.append(SORTBY);
    if (sortAsc)
     sql.append(" ASC ");
    else
     sql.append(" DESC ");

    DBUtil.appendLimit(sql, from, limit);

    return getBySql(sql.toString());
  }

  protected Iterator getBySql(String sql)
  {
    Vector v = new Vector();
    try
    {
      // Use the transaction, pass the DbAdapter to the bean
      DbAdapter db = new DbAdapter(true);
      BoxResourceCatEntryBean bean = new BoxResourceCatEntryBean(null,db);
      Iterator it = bean.findBySql(sql);
      while (it.hasNext())
      {
        BaseIntKey key = (BaseIntKey) it.next();
        bean = new BoxResourceCatEntryBean(key,db);
        bean.load();
        BoxResourceCatEntry entry = new BoxResourceCatEntry();
        entry.copyFrom(bean);
        v.add(entry);
      }
      db.close();
      db = null;
    }
    catch (Exception e)
    {
      e.printStackTrace();
    //TODO: Log
    }
    return v.iterator();
  }
}
